package jgl.vajra.operator.part.mine.activity;

import android.graphics.Typeface;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.TextView;

import androidx.fragment.app.Fragment;
import androidx.viewpager.widget.ViewPager;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import jgl.vajra.operator.R;
import jgl.vajra.operator.bean.MyTeamPeopleNumBean;
import jgl.vajra.operator.databinding.ActivityMyTeamBinding;
import jgl.vajra.operator.manager.AccountManager;
import jgl.vajra.operator.manager.FragmentManager;
import jgl.vajra.operator.part.mine.contract.MyTeamContract;
import jgl.vajra.operator.part.mine.fragment.MyTeamAllListFragment;
import jgl.vajra.operator.part.mine.viewModel.MyTeamViewModel;
import jgl.vajra.operator.widget.base.BaseFragmentAdapter;
import jgl.vajra.operator.widget.mvvm.factory.CreateViewModel;
import jgl.vajra.operator.widget.mvvm.view.BaseMVVMActivity;
import jgl.vajra.operator.widget.utils.UIUtils;
import jgl.vajra.operator.widget.view.tablayout.TabLayout;

@CreateViewModel(MyTeamViewModel.class)
public class MyTeamActivity extends BaseMVVMActivity<MyTeamViewModel, ActivityMyTeamBinding> implements MyTeamContract.View {

    private List<String> titleList;
    private int currentPosition;

    @Override
    public int getLayoutId() {
        return R.layout.activity_my_team;
    }

    @Override
    public void initView() {
        super.initView();
        initTab();
        requestNetData();
    }

    private void requestNetData() {
        Map<String, Object> map = new HashMap<>();
        map.put("Authorization", AccountManager.getInstance().getLoginData(UIUtils.getContext()).authorization);
        mViewModel.getMyTeamPeopleNum(map);
    }

    private void initTab() {
        titleList = new ArrayList<>();
        titleList.add("全部团队");
        titleList.add("机佬团队");
        titleList.add("包工佬团队");
        titleList.add("大师傅团队");
        for (int i = 0; i < titleList.size(); i++) {
            final TabLayout.Tab tab = mBinding.tabLayout.newTab();
            tab.setCustomView(getTabView(i));
            final View tabView = (View) tab.getCustomView().getParent();
            tabView.setTag(i);
            mBinding.tabLayout.addTab(tab);
        }

        List<Fragment> fragmentList = new ArrayList<>();
        MyTeamAllListFragment listFragment1 = FragmentManager.getMyTeamAllListFragment(1);
        fragmentList.add(listFragment1);
        MyTeamAllListFragment listFragment2 = FragmentManager.getMyTeamAllListFragment(2);
        fragmentList.add(listFragment2);
        MyTeamAllListFragment listFragment3 = FragmentManager.getMyTeamAllListFragment(3);
        fragmentList.add(listFragment3);
        MyTeamAllListFragment listFragment4 = FragmentManager.getMyTeamAllListFragment(4);
        fragmentList.add(listFragment4);
        BaseFragmentAdapter adapter = new BaseFragmentAdapter(getSupportFragmentManager(), titleList, fragmentList, 1);
        mBinding.viewPager.setAdapter(adapter);
//        mBinding.tabLayout.setupWithViewPager(mBinding.viewPager);
        updateTabTextView(mBinding.tabLayout.getTabAt(0), true);
    }

    private void setMessageCount(int num, TextView textView) {
        textView.setText(num + "人");
    }

    @Override
    public void initEvent() {
        super.initEvent();
        mBinding.tabLayout.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
            @Override
            public void onTabSelected(TabLayout.Tab tab) {
                updateTabTextView(tab, true);
                mBinding.viewPager.setCurrentItem(tab.getPosition());
            }

            @Override
            public void onTabUnselected(TabLayout.Tab tab) {
                updateTabTextView(tab, false);
            }

            @Override
            public void onTabReselected(TabLayout.Tab tab) {
            }
        });
        mBinding.viewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
            }

            @Override
            public void onPageSelected(int position) {
                currentPosition = position;
                mBinding.tabLayout.getTabAt(position).select();
            }

            @Override
            public void onPageScrollStateChanged(int state) {
            }
        });
    }

    private void updateTabTextView(TabLayout.Tab tab, boolean isSelect) {
        TextView tabSelect = tab.getCustomView().findViewById(R.id.tv_title);
        tabSelect.setTypeface(Typeface.defaultFromStyle(isSelect ? Typeface.BOLD : Typeface.NORMAL));
        tabSelect.setTextColor(UIUtils.getColor(isSelect ? R.color.black_32 : R.color.gray_99));
    }

    private View getTabView(int currentPosition) {
        View view = LayoutInflater.from(this).inflate(R.layout.tab_my_team, null);
        TextView textView = view.findViewById(R.id.tv_title);
        textView.setText(titleList.get(currentPosition));
        return view;
    }

    @Override
    public void returnMyTeamPeopleNumData(MyTeamPeopleNumBean data) {
        setMessageCount(data.totalCount, mBinding.tabLayout.getTabAt(0).getCustomView().findViewById(R.id.tv_num));
        setMessageCount(data.ownerCount, mBinding.tabLayout.getTabAt(1).getCustomView().findViewById(R.id.tv_num));
        setMessageCount(data.clientCount, mBinding.tabLayout.getTabAt(2).getCustomView().findViewById(R.id.tv_num));
        setMessageCount(data.driverCount, mBinding.tabLayout.getTabAt(3).getCustomView().findViewById(R.id.tv_num));
    }
}
